﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EntityDesigner2.Controls
{
    public class EdDataGridView : DataGridView
    {
        public bool IsExternalControl { get; set; }

        private int _scorllRowIndex;
        private int _scorllColumnIndex;

        public EdDataGridView()
        {
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            this.IsExternalControl = false;
        }

        /// <summary>
        /// 存储滚动条位置
        /// </summary>
        public void RestoreScroll()
        {
            _scorllColumnIndex = this.FirstDisplayedScrollingColumnIndex;
            _scorllRowIndex = this.FirstDisplayedScrollingRowIndex;
            if (_scorllRowIndex < 0) _scorllRowIndex = 0;
        }

        /// <summary>
        /// 重置滚动条位置
        /// </summary>
        public void ResetScroll()
        {
            this.FirstDisplayedScrollingColumnIndex = _scorllColumnIndex;
            if (this.Rows.Count > 0) this.FirstDisplayedScrollingRowIndex = _scorllRowIndex;
            //this.VerticalScrollingOffset;
        }

        //protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        //{
        //    switch (e.KeyCode)
        //    {
        //        case Keys.Up:
        //        case Keys.Down:
        //        case Keys.Enter:
        //            return false;
        //    }
        //    return base.ProcessDataGridViewKey(e);
        //}

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Up:
                case Keys.Down:
                    if (IsExternalControl) return true;
                    break;
                case Keys.Enter:
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
